$(function () {
    getUserInfo()

    //退出登录
    $('.logout').on('click', function () {
    
        layer.confirm('是否确认退出?', {icon: 3, title:'提示'}, function(index){
            location.href = '/login.html'
            localStorage.removeItem('token')
            layer.close(index);
         });
    })
})

// 发送Ajax全局函数
function getUserInfo() {
    axios({
        url:'/my/userinfo',
        method:'get',
        headers: {
            Authorization:localStorage.getItem('token')
        }
    }).then(({ data: res }) => {
        console.log(res);
        if (res.status != 0) {
            return layui.layer.msg(res.message)
        }
        renderAvatar(res.data)
    })
}
function renderAvatar(user) {
    let name = user.nikename || user.username
    $('.welcome').html('欢迎 ' + name)

    if (user.user_pic != null) {
        $('.layui-nav-img').show().prop('src', user.user_pic)
        $('.avatar-text').hide()
    } else {
        let first = user.username[0].toUpperCase()
        $('.layui-nav-img').hide()
        $('.avatar-text').show().html(first)
    }
}